package org.sardine.beans.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.sardine.beans.BeanPostProcessor;
import org.sardine.beans.DefaultBeanRegistry;
import org.sardine.beans.DefaultXmlReader;
import org.sardine.beans.ObjectFactory;
import org.sardine.beans.SimpleXmlDefinitionParser;
import org.sardine.beans.XmlParser;
import org.sardine.beans.XmlValidationMode;
import org.sardine.util.Assert;

public class DefaultElmBeanDefinitionRegistry extends DefaultBeanRegistry implements ElmBeanDefinitionRegistry{
	
	private final Map<String,ElmBeanDefinition> beanDefinitions = new HashMap<String,ElmBeanDefinition>();
	//private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();
	private final List<String> beanPostProcessorIds = new ArrayList<String>();
	private final DefaultXmlReader reader = new DefaultXmlReader(XmlValidationMode.XSD);
	private final String[] configLocations;
	
	public DefaultElmBeanDefinitionRegistry(String[] configLocations){
		this.configLocations = configLocations;
		initialize();
	}
	
	protected void initialize(){
		
		loadElmBeanDefinition();
		//instantiateAllBeans();
	}
	
	private void loadElmBeanDefinition(){
		XmlParser parser = new SimpleXmlDefinitionParser(this);
		reader.setParser(parser);
		reader.loadXmlFiles(configLocations);
	}
	
	

	public void registerElmBeanDefinition(String id, ElmBeanDefinition beanDefinition) {
		this.beanDefinitions.put(id, beanDefinition);
	}

	public void removeElmBeanDefinition(String id) {
		this.beanDefinitions.remove(id);
	}

	public ElmBeanDefinition getElmBeanDefinition(String id) {
		return this.beanDefinitions.get(id);
	}

	public boolean containsElmBeanDefinition(String id) {
		return this.beanDefinitions.containsKey(id);
	}

	public int getElmBeanDefinitionCount() {
		return this.beanDefinitions.size();
	}

	public Map<String, ElmBeanDefinition> getBeanDefinitions() {
		return beanDefinitions;
	}

	public List<String> getBeanPostProcessorIds() {
		return beanPostProcessorIds;
	}
	
	protected void registerBeanPostProcessorId(String postProcessorId){
		this.beanPostProcessorIds.add(postProcessorId);
	}
	
	/*protected void registerBeanPostProcessor(BeanPostProcessor postProcessor){
		this.beanPostProcessors.add(postProcessor);
	}

	public List<BeanPostProcessor> getBeanPostProcessors() {
		return beanPostProcessors;
	}*/
	
	

}
